import os,sys,time
sys.path.append("../")
from am import AndroidAm
from logcat import logcat
import subprocess
import signal

devices=[]

def sigint_handler(signum, frame):
  global is_sigint_up
  is_sigint_up = True
  print 'catched interrupt signal!'

if __name__=='__main__':
	am=AndroidAm()
	logcat=logcat()
	
	signal.signal(signal.SIGINT, sigint_handler)
	signal.signal(signal.SIGTERM, sigint_handler)
	is_sigint_up = False
	os.system("adb devices > device_t")
	
	f=open('./device_t','r')
	for line in iter(f):
		if line.find("List")==-1 and line.find('device')!=-1:
			dev_id=line.split()[0]
			devices.append(dev_id)
	f.close()
	os.system("del device_t")
	print '##################################'
	while True:
		for dev_id in devices:
			am.ConnectWifi(dev_id,"1122334455","12345678")
			time.sleep(10)
			am.closeWifi(dev_id)
			time.sleep(10)
			if is_sigint_up:
				break
			

	
	